package com.ferris.redbook.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ferris.redbook.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    User selectByEmail(@Param("email") String email);
    Integer countByEmail(@Param("email") String email);
    Integer countByNickname(@Param("nickname") String nickname);
    int insertUser(User user);

    /**
     * 统计用户关注数
     */
    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{userId}")
    Long countFollows(Long userId);

    /**
     * 统计用户粉丝数
     */
    @Select("SELECT COUNT(*) FROM follows WHERE following_id = #{userId}")
    Long countFans(Long userId);

    /**
     * 统计用户获赞数（通过帖子点赞数汇总）
     */
    @Select("SELECT COALESCE(SUM(like_count), 0) FROM posts WHERE user_id = #{userId}")
    Long countLikedPosts(Long userId);
}